package com.cskaoyan.market.aspect;

import com.cskaoyan.market.bo.GoodBo;
import com.cskaoyan.market.db.domain.*;
import com.cskaoyan.market.db.mapper.MarketCategoryMapper;
import com.cskaoyan.market.db.mapper.MarketGoodsMapper;
import com.cskaoyan.market.db.mapper.MarketKeywordMapper;
import com.cskaoyan.market.db.mapper.MarketSearchHistoryMapper;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.session.Session;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.List;


/**
 * @author: YaLiFeng
 * @description: TODO
 * @date: 2024/5/20 20:52
 */
@Component
@Aspect
public class SearchAspect {
    @Autowired
    MarketSearchHistoryMapper marketSearchHistoryMapper;
    @Autowired
    MarketKeywordMapper marketKeywordMapper;
    @Autowired
    MarketGoodsMapper marketGoodsMapper;
    @Autowired
    MarketCategoryMapper marketCategoryMapper;

    @Pointcut("execution(* com.cskaoyan..WxGoodsController.list(..))")
//@Pointcut("execution(* com.cskaoyan..service.WxGoodsServiceImpl.*List(..))")
    public void pt1(){

    }
    @Pointcut("execution(* com.cskaoyan..service.WxGoodsService.goodsList1(..))")
    public void keyWordHistory(){

    }
    @Before("pt1()")
    public void beforeAdvice(JoinPoint joinPoint) {
        //获取所有的参数值的数组
        Object[] args = joinPoint.getArgs();
        Signature signature = joinPoint.getSignature();
        MethodSignature methodSignature = (MethodSignature) signature;
        //活到方法的所有参数名称的字符串数组
        String[] parameterName = methodSignature.getParameterNames();
        try {
            //通过需要获取的参数名称的下表获取到对应的值
            int argGoodBoIndex = ArrayUtils.indexOf(parameterName, "goodBo");
            if (argGoodBoIndex != -1) {
                GoodBo goodBo = (GoodBo) args[argGoodBoIndex];
                MarketSearchHistoryExample marketSearchHistoryExample = new MarketSearchHistoryExample();
                Session session = SecurityUtils.getSubject().getSession();
                MarketUser user = (MarketUser) session.getAttribute("user");
                if (user == null) {
                    return;
                }
                //加入历史搜索
                MarketSearchHistoryExample.Criteria criteriaHistory = marketSearchHistoryExample.createCriteria();
                if (!StringUtils.isEmpty(goodBo.getKeyword())) {
                    criteriaHistory.andKeywordEqualTo(goodBo.getKeyword());

                    MarketSearchHistory marketSearchHistory1 = marketSearchHistoryMapper.selectOneByExample(marketSearchHistoryExample);
                    //查询出来的历史关键词表
                    if (marketSearchHistory1 == null) {
                        MarketSearchHistory marketSearchHistory = new MarketSearchHistory();
                        LocalDateTime localDateTime = LocalDateTime.now();
                        marketSearchHistory.setAddTime(localDateTime);
                        marketSearchHistory.setDeleted(false);
                        marketSearchHistory.setKeyword(goodBo.getKeyword());
                        marketSearchHistory.setUserId(user.getId());
                        marketSearchHistoryMapper.insertSelective(marketSearchHistory);
                    }
                    else {
                        marketSearchHistory1.setDeleted(false);
                        marketSearchHistory1.setUpdateTime(LocalDateTime.now());
                        marketSearchHistoryMapper.updateByExample(marketSearchHistory1, marketSearchHistoryExample);


                    }


                }
            }
        }catch(Exception e){
            e.printStackTrace();

        }
    }
    @AfterReturning(pointcut ="keyWordHistory()",returning = "returnValue")
    public void keyWordReturn(JoinPoint joinPoint,Object returnValue){
        Object[] args = joinPoint.getArgs();
        Signature signature = joinPoint.getSignature();
        MethodSignature methodSignature = (MethodSignature) signature;
        //活到方法的所有参数名称的字符串数组
        String[] parameterName = methodSignature.getParameterNames();
        try {
            //通过需要获取的参数名称的下表获取到对应的值
            int argGoodBoIndex = ArrayUtils.indexOf(parameterName, "goodBo");
            if (argGoodBoIndex != -1) {
                GoodBo goodBo = (GoodBo) args[argGoodBoIndex];
                MarketSearchHistoryExample marketSearchHistoryExample = new MarketSearchHistoryExample();
                Session session = SecurityUtils.getSubject().getSession();
                MarketUser user = (MarketUser) session.getAttribute("user");
                if (user == null) {
                    return;
                }
                //如果获取的结果不为空，则将此关键词加入keyword
                //将商品的关键词插入
                List<MarketGoods> goodsList= (List<MarketGoods>) returnValue;
                if (goodsList!=null&&goodsList.size()!=0) {
                    //获取关键词历史
                    MarketKeywordExample marketKeywordExample = new MarketKeywordExample();
                    MarketKeywordExample.Criteria criteria = marketKeywordExample.createCriteria();
                    criteria.andKeywordEqualTo(goodBo.getKeyword());
                    MarketKeyword marketKeyword = marketKeywordMapper.selectOneByExample(marketKeywordExample);
                    if(marketKeyword==null){
                        MarketKeyword marketKeyword1=new MarketKeyword();
                        marketKeyword1.setKeyword(goodBo.getKeyword());
                        marketKeyword1.setAddTime(LocalDateTime.now());
                        marketKeyword1.setUpdateTime(LocalDateTime.now());
                        marketKeyword1.setDeleted(false);
                        marketKeywordMapper.insertSelective(marketKeyword1);
                    }
                    //关键词加入商品
                    for (int i = 0; i <goodsList.size() ; i++) {
                        MarketGoodsExample marketGoodsExample=new MarketGoodsExample();
                        MarketGoodsExample.Criteria criteria1 = marketGoodsExample.createCriteria();
                        criteria1.andIdEqualTo(goodsList.get(i).getId());
                        MarketGoods marketGoods=marketGoodsMapper.selectByPrimaryKey(goodsList.get(i).getId());
                        marketGoods.setKeywords(goodBo.getKeyword()+",");
                        marketGoodsMapper.updateByExample(marketGoods,marketGoodsExample);
                    }
                    //类目加入关键词
                    for (int i = 0; i < goodsList.size(); i++) {
                        MarketGoods marketGoods=marketGoodsMapper.selectByPrimaryKey(goodsList.get(i).getId());
                        MarketCategory marketCategory=new MarketCategory();
                        MarketCategoryExample marketCategoryExample=new MarketCategoryExample();
                        MarketCategoryExample.Criteria criteria1 = marketCategoryExample.createCriteria();
                        criteria1.andIdEqualTo(goodsList.get(i).getCategoryId());
                        marketCategory=marketCategoryMapper.selectOneByExample(marketCategoryExample);
                        marketCategory.setKeywords(goodBo.getKeyword()+",");


                    }
                }
            }
        }catch(Exception e){
            e.printStackTrace();

        }



    }







}
